﻿using MongoDB.Bson;
using MongoDB.Driver;
using System;

namespace MongoDB_Simple_Demo
{
    class Program
    {
        static void Main(string[] args)
        {

            MongoClient client = new MongoClient("mongodb://127.0.0.1");

            IMongoDatabase db = client.GetDatabase("TestDb1");

            //IMongoCollection<Person> persons = db.GetCollection<Person>("Persons");

            //Person p1 = new Person
            //{
            //    Id = 1,
            //    Name = "张三",
            //    Age = 18
            //};

            //persons.InsertOne(p1);

            //await persons.InsertOneAsync(p1);

            //IMongoCollection<Dog> gougoumen = db.GetCollection<Dog>("gougoumen");
            //Dog dog = new Dog()
            //{
            //    Name = "小黄",
            //    Age=3
            //};
            //Console.WriteLine(dog.Id);
            //gougoumen.InsertOne(dog);
            //Console.WriteLine(dog.Id);
            //Console.WriteLine("插入成功!");


            //MongoDB是用json保存的,因此也可以直接以json格式插入,用BsonDocument来代表:
            //IMongoCollection<BsonDocument> dogs = db.GetCollection<BsonDocument>("gougoumen");
            //string json = "{id:666,name:'dog',age:5,height:0.5,color:'red'}";
            //BsonDocument d1 = BsonDocument.Parse(json);
            //dogs.InsertOne(d1);

            //Console.WriteLine("插入成功!");


            //还可以插入有嵌套关系的对象，比如学生和老师，注意不会有表间关系，都是存到一个集合中，注意和关系库不一样。
            IMongoCollection<Dog> dogs = db.GetCollection<Dog>("gougoumen");
            Dog d1 = new Dog();
            d1.Name = "京巴";
            d1.Age = 23;
            Person p1 = new Person() { Id = 8, Name = "小主", Age = 18 };
            d1.Master = p1;

            dogs.InsertOne(d1);
            Console.WriteLine("插入成功!");

            Console.ReadKey();

        }
    }
}
